<template>
  <BasicModal
    v-bind="$attrs"
    destoryOnClose
    width="45%"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { conditionFormSchema, stateIdOptions } from './data';

  export default defineComponent({
    name: 'DeptModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const { createMessage } = useMessage();

      const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
        labelWidth: 100,
        baseColProps: { span: 12 },
        schemas: conditionFormSchema,
        showActionButtonGroup: false,
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (data.record) {
          console.log('data.record', data.record);

          const { choiceList, recipientID, recipients } = data.record;

          const options = choiceList.map((item) => {
            return {
              label: item.name || '无',
              value: item.id,
            };
          });

          const recipientVal = [
            {
              userId: recipientID,
              name: recipients,
            },
          ];

          const schemasArr = [
            {
              field: 'activityChoice',
              componentProps: {
                options,
              },
            },
          ];

          updateSchema(schemasArr);

          setFieldsValue({ ...data.record, recipientID: recipientVal });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增状态' : '修改状态'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });

          console.log('values', values);

          const { recipientID, stateID } = values;

          const recipientObj = recipientID[0];

          const showTitle = stateIdOptions.find((item) => item.value == stateID)?.label;

          console.log('recipientObj', recipientObj);

          const successVal = {
            ...values,
            showTitle,
            inceptDate: values.inceptDate,
            readDate: values.readDate ? values.readDate : '',
            finishedDate: values.finishedDate ? values.finishedDate : '',
            callDate: values.callDate ? values.callDate : '',
            recipientID: recipientObj.userId,
            recipients: recipientObj.name,
          };

          closeModal();
          createMessage.success('操作成功');
          emit('success', successVal);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>
